package com.icedbrew.appfuel.persistence.service.impl;

import com.icedbrew.appfuel.AppFuelException;
import com.icedbrew.appfuel.DatabaseException;
import com.icedbrew.appfuel.persistence.dao.IDao;
import com.icedbrew.appfuel.persistence.entity.BaseEntity;
import com.icedbrew.appfuel.persistence.service.IPersistenceService;

import java.util.List;

/**
 * Base implementation of the {@link com.icedbrew.appfuel.persistence.service.IPersistenceService} interface.
 *
 * @author  arjay.nacion
 * @since   1.0
 */
public abstract class BasePersistenceService<E extends BaseEntity>
        implements IPersistenceService<E> {

    public final void addOrUpdate(E entity) {
	    try {
		    getDao().addOrUpdate(entity);
	    } catch (DatabaseException dex) {
		    throw new AppFuelException(dex);
	    }
    }

    public final void delete(E entity) {
	    try {
		    getDao().delete(entity);
	    } catch (DatabaseException dex) {
		    throw new AppFuelException(dex);
	    }
    }

    public final void deleteById(Long id) {
	    try {
		    getDao().deleteById(id);
	    } catch (DatabaseException dex) {
		    throw new AppFuelException(dex);
	    }
    }

    public final E findById(final Long id) {
        try {
	        return getDao().findById(id);
        } catch (DatabaseException dex) {
	        throw new AppFuelException(dex);
        }
    }

    public final List<E> findAll() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public final List<E> findBatch(int start, int size) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public final Long countAll() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    protected abstract IDao<E> getDao();
}
